<?php
header('content-type:text/html;charset=utf-8');

$con = mysql_connect("localhost", "root", "root");
if (!$con) {
    die('Could not connect: ' . mysql_error());
}

mysql_select_db("test", $con);

//检索三个数据，单词按月份分布，按星分布，最近n天每天的单词数
//$res = mysql_query("SELECT count(id) as `word` ,DATE_FORMAT(lastdate,'%Y-%m') as `month` from table01 group by DATE_FORMAT(lastdate,'%Y-%m');");
$sql = "SELECT DATE_FORMAT(lastdate,'%Y-%m') as `month`, count(id) as `word`  from table01 group by DATE_FORMAT(lastdate,'%Y-%m') order by `month` desc ;";
showtable($sql, $con);
$sql2 = "SELECT star  ,count(id) as `word`  from table01 group by star;";
showtable($sql2, $con);
$sql3 = "SELECT lastdate_word as `date`,count(id) as `word` from `wordhistory` group by lastdate_word order by lastdate_word desc limit 15  ;";
showtable($sql3, $con);

function showtable($sql, $con)
{
    $res = mysql_query($sql);
    $rows = mysql_affected_rows($con);//获取行数
    $colums = mysql_num_fields($res);//获取列数
    //echo $sql;
        echo '<table  border="1"><tr>';
        //for($i=0; $i < 100; $i++){
    for ($i = 0; ($i < $colums) and ($i < 100); $i++) {
        $field_name = mysql_field_name($res, $i);
        echo "<th>$field_name</th>";
    }
    echo "</tr>";
    while ($row = mysql_fetch_row($res)) {
        echo "<tr>";
        for ($i = 0; $i < $colums and ($i < 100); $i++) {
            echo "<td>$row[$i]</td>";
        }
        echo "</tr>";
    }
    echo "</table></br>";
}



mysql_close($con);
?> 
